# 18_jes2001_2016_participation_in_internet.R
# Purpose: Ideological Extremism and Political Participation in Japan
# Created: 2021-3-14 Taka-aki Asano
# Last Modified: 2021-10-9

# package
require("haven")
require("dplyr")
require("tidyr")
require("ggplot2")
require("texreg")
quartzFonts(HiraKaku = quartzFont(rep('Hiragino Kaku Gothic Pro W3', 4)))
theme_set(theme_classic(base_size = 12, base_family = 'HiraKaku'))


# 2003
## merge
JES2003_Ideology <- JES2003_Respondent[,c("ID", "Ideology")]
JES2003_Ideology <- as.data.frame(JES2003_Ideology)
JES2003_Ideology <- left_join(JES2003_Ideology, 
                              JES2003[,c("ID", "GENDER", "EDUCAT03", "BORNY", "E11_12", "D8")], 
                              by = "ID")

## recode
table(JES2003_Ideology$E11_12)
JES2003_Ideology$Ideology_strong <- abs(JES2003_Ideology$Ideology - 
                                          median(JES2003_Ideology$Ideology, na.rm = TRUE))
JES2003_Ideology$D8[JES2003_Ideology$D8 %in% c(88, 99)] <- NA
JES2003_Ideology$Support <- ifelse(JES2003_Ideology$D8 == 9, 0, 1)
JES2003_Ideology$Female <- ifelse(JES2003_Ideology$GENDER == 2, 1, 0)
JES2003_Ideology$EDUCAT03[JES2003_Ideology$EDUCAT03 == 9] <- NA
JES2003_Ideology$College <- ifelse(JES2003_Ideology$EDUCAT03 == 4, 1, 0)
JES2003_Ideology$Age <- 2003 - JES2003_Ideology$BORNY

## regression
glm2003 <- glm(E11_12 ~ Ideology + Ideology_strong + Support + Female + College + Age, 
               data = JES2003_Ideology, family = binomial(link = "logit"))
summary(glm2003)


# 2005
## merge
JES2005_Ideology <- JES2005_Respondent[,c("ID", "Ideology")]
JES2005_Ideology <- as.data.frame(JES2005_Ideology)
JES2005_Ideology <- left_join(JES2005_Ideology, 
                              JES2005[,c("ID", "GENDER", "EDUCAT05", "BORNY", "K21_12", "J10")], 
                              by = "ID")

## recode
table(JES2005_Ideology$K21_12)
JES2005_Ideology$Ideology_strong <- abs(JES2005_Ideology$Ideology - 
                                          median(JES2005_Ideology$Ideology, na.rm = TRUE))
JES2005_Ideology$J10[JES2005_Ideology$J10 %in% c(88, 99)] <- NA
JES2005_Ideology$Support <- ifelse(JES2005_Ideology$J10 == 9, 0, 1)
JES2005_Ideology$Female <- ifelse(JES2005_Ideology$GENDER == 2, 1, 0)
JES2005_Ideology$EDUCAT05[JES2005_Ideology$EDUCAT05 == 9] <- NA
JES2005_Ideology$College <- ifelse(JES2005_Ideology$EDUCAT05 == 4, 1, 0)
JES2005_Ideology$Age <- 2005 - JES2005_Ideology$BORNY

## regression
glm2005 <- glm(K21_12 ~ Ideology + Ideology_strong + Support + Female + College + Age, 
               data = JES2005_Ideology, family = binomial(link = "logit"))
summary(glm2005)


# 2007
## merge
JES2007_Ideology <- JES2007_Respondent[,c("ID", "Ideology")]
JES2007_Ideology <- as.data.frame(JES2007_Ideology)
JES2007_Ideology <- left_join(JES2007_Ideology, 
                              JES2007[,c("ID", "F1", "F4", "F2_AGE", "Q51x12", "Q20")], 
                              by = "ID")

## recode
table(JES2007_Ideology$Q51x12)
JES2007_Ideology$Ideology_strong <- abs(JES2007_Ideology$Ideology - 
                                          median(JES2007_Ideology$Ideology, na.rm = TRUE))
JES2007_Ideology$Q20[JES2007_Ideology$Q20 %in% c(88, 99)] <- NA
JES2007_Ideology$Support <- ifelse(JES2007_Ideology$Q20 == 9, 0, 1)
JES2007_Ideology$Female <- ifelse(JES2007_Ideology$F1 == 2, 1, 0)
JES2007_Ideology$F4[JES2007_Ideology$F4 == 9] <- NA
JES2007_Ideology$College <- ifelse(JES2007_Ideology$F4 == 4, 1, 0)

## regression
glm2007 <- glm(Q51x12 ~ Ideology + Ideology_strong + Support + Female + College + F2_AGE, 
               data = JES2007_Ideology, family = binomial(link = "logit"))
summary(glm2007)


# 2009
## merge
JES2009_Ideology <- JES2009_Respondent[,c("ID", "Ideology")]
JES2009_Ideology <- as.data.frame(JES2009_Ideology)
JES2009_Ideology <- left_join(JES2009_Ideology, 
                              JES2009[,c("ID", "F1", "F4", "F2_AGE", "Q10")], 
                              by = "ID")
JES2009_2 <- read_sav(
  "~/Dropbox/Dataset/JESIV/0999_wave3.sav", 
  user_na = FALSE
)
JES2009_Ideology <- left_join(JES2009_Ideology, 
                              JES2009_2[,c("ID", "Q23x12")], 
                              by = "ID")

## recode
table(JES2009_Ideology$Q23x12)
JES2009_Ideology$Ideology_strong <- abs(JES2009_Ideology$Ideology - 
                                          median(JES2009_Ideology$Ideology, na.rm = TRUE))
JES2009_Ideology$Q10[JES2009_Ideology$Q10 %in% c(88, 99)] <- NA
JES2009_Ideology$Support <- ifelse(JES2009_Ideology$Q10 == 9, 0, 1)
JES2009_Ideology$Female <- ifelse(JES2009_Ideology$F1 == 2, 1, 0)
JES2009_Ideology$F4[JES2009_Ideology$F4 == 9] <- NA
JES2009_Ideology$College <- ifelse(JES2009_Ideology$F4 == 4, 1, 0)

## regression
glm2009 <- glm(Q23x12 ~ Ideology + Ideology_strong + Support + Female + College + F2_AGE, 
               data = JES2009_Ideology, family = binomial(link = "logit"))
summary(glm2009)


# 2010
## merge
JES2010_Ideology <- JES2010_Respondent[,c("ID", "Ideology")]
JES2010_Ideology <- as.data.frame(JES2010_Ideology)
JES2010_Ideology <- left_join(JES2010_Ideology, 
                              JES2010[,c("ID", "F1", "F4", "F2_AGE", "Q8")], 
                              by = "ID")
JES2010_2 <- read_sav(
  "~/Dropbox/Dataset/JESIV/0999_wave6.sav", 
  user_na = FALSE
)
JES2010_Ideology <- left_join(JES2010_Ideology, 
                              JES2010_2[,c("ID", "Q29x12")], 
                              by = "ID")

## recode
table(JES2010_Ideology$Q29x12)
JES2010_Ideology$Ideology_strong <- abs(JES2010_Ideology$Ideology - 
                                          median(JES2010_Ideology$Ideology, na.rm = TRUE))
JES2010_Ideology$Q8[JES2010_Ideology$Q8 %in% c(88, 99)] <- NA
JES2010_Ideology$Support <- ifelse(JES2010_Ideology$Q8 == 11, 0, 1)
JES2010_Ideology$Female <- ifelse(JES2010_Ideology$F1 == 2, 1, 0)
JES2010_Ideology$F4[JES2010_Ideology$F4 == 9] <- NA
JES2010_Ideology$College <- ifelse(JES2010_Ideology$F4 == 4, 1, 0)

## regression
glm2010 <- glm(Q29x12 ~ Ideology + Ideology_strong + Support + Female + College + F2_AGE, 
               data = JES2010_Ideology, family = binomial(link = "logit"))
summary(glm2010)


# 2012
## merge
JES2012_Ideology <- JES2012_Respondent[,c("id", "Ideology")]
JES2012_Ideology <- as.data.frame(JES2012_Ideology)
JES2012_Ideology <- left_join(JES2012_Ideology, 
                              JES2012[,c("id", "f1", "f4", "f2", "q60s2_15", "q54")], 
                              by = "id")

## recode
JES2012_Ideology$q60s2_15[is.na(JES2012_Ideology$q60s2_15)] <- 0
JES2012_Ideology$Ideology_strong <- abs(JES2012_Ideology$Ideology - 
                                          median(JES2012_Ideology$Ideology, na.rm = TRUE))
JES2012_Ideology$Support <- ifelse(JES2012_Ideology$q54 == 15, 0, 1)
JES2012_Ideology$Female <- ifelse(JES2012_Ideology$f1 == 2, 1, 0)
JES2012_Ideology$f4[JES2012_Ideology$f4 == 9] <- NA
JES2012_Ideology$College <- ifelse(JES2012_Ideology$f4 == 4, 1, 0)

## regression
glm2012 <- glm(q60s2_15 ~ Ideology + Ideology_strong + Support + Female + College + f2, 
               data = JES2012_Ideology, family = binomial(link = "logit"))
summary(glm2012)


# 2013
## merge
JES2013_Ideology <- JES2013_Respondent[,c("id", "Ideology")]
JES2013_Ideology <- as.data.frame(JES2013_Ideology)
JES2013_Ideology <- left_join(JES2013_Ideology, 
                              JES2013[,c("id", "af1", "af4", "af2_age", "b25_12", "a12x1")], 
                              by = "id")

## recode
table(JES2013_Ideology$b25_12)
JES2013_Ideology$Ideology_strong <- abs(JES2013_Ideology$Ideology - 
                                          median(JES2013_Ideology$Ideology, na.rm = TRUE))
JES2013_Ideology$Support <- ifelse(JES2013_Ideology$a12x1 == 8, 0, 1)
JES2013_Ideology$Female <- ifelse(JES2013_Ideology$af1 == 2, 1, 0)
JES2013_Ideology$af4[JES2013_Ideology$af4 == 9] <- NA
JES2013_Ideology$College <- ifelse(JES2013_Ideology$af4 == 4, 1, 0)

## regression
glm2013 <- glm(b25_12 ~ Ideology + Ideology_strong + Support + Female + College + af2_age, 
               data = JES2013_Ideology, family = binomial(link = "logit"))
summary(glm2013)


# 2014
## merge
JES2014_Ideology <- JES2014_Respondent[,c("id", "Ideology")]
JES2014_Ideology <- as.data.frame(JES2014_Ideology)
JES2014_Ideology <- left_join(JES2014_Ideology, 
                              JES2014[,c("id", "sex", "a55", "age", "b26_12", "a7")], 
                              by = "id")

## recode
table(JES2014_Ideology$b26_12)
JES2014_Ideology$Ideology_strong <- abs(JES2014_Ideology$Ideology - 
                                          median(JES2014_Ideology$Ideology, na.rm = TRUE))
JES2014_Ideology$Support <- ifelse(JES2014_Ideology$a7 == 9, 0, 1)
JES2014_Ideology$Female <- ifelse(JES2014_Ideology$sex == 2, 1, 0)
JES2014_Ideology$a55[JES2014_Ideology$a55 == 9] <- NA
JES2014_Ideology$College <- ifelse(JES2014_Ideology$a55 == 4, 1, 0)

## regression
glm2014 <- glm(b26_12 ~ Ideology + Ideology_strong + Support + Female + College + age, 
               data = JES2014_Ideology, family = binomial(link = "logit"))
summary(glm2014)


# 2016
## merge
JES2016_Ideology <- JES2016_Respondent[,c("id", "Ideology")]
JES2016_Ideology <- as.data.frame(JES2016_Ideology)
JES2016_Ideology <- left_join(JES2016_Ideology, 
                              JES2016[,c("id", "b_F1_SEX", "F4", "b_F2_AGE", "b_Q12_12", "Q5")], 
                              by = "id")

## recode
table(JES2016_Ideology$b_Q12_12)
JES2016_Ideology$Ideology_strong <- abs(JES2016_Ideology$Ideology - 
                                          median(JES2016_Ideology$Ideology, na.rm = TRUE))
JES2016_Ideology$Support <- ifelse(JES2016_Ideology$Q5 == 7, 0, 1)
JES2016_Ideology$Female <- ifelse(JES2016_Ideology$b_F1_SEX == 2, 1, 0)
JES2016_Ideology$F4[JES2016_Ideology$F4 == 9] <- NA
JES2016_Ideology$College <- ifelse(JES2016_Ideology$F4 == 4, 1, 0)

## regression
glm2016 <- glm(b_Q12_12 ~ Ideology + Ideology_strong + Support + Female + College + b_F2_AGE, 
               data = JES2016_Ideology, family = binomial(link = "logit"))
summary(glm2016)


# plot
glmAll <- data.frame(
  Year = rep(c(2003, 2005, 2007, 2009, 2010, 2012, 2013, 2014, 2016), each = 7), 
  Variable = rep(c("Intercept", "Ideology", "Ideological Extremism", 
                   "Partisanship", "Female", "College Education", "Age"), times = 9), 
  Estimate = c(coef(glm2003), coef(glm2005), coef(glm2007), 
               coef(glm2009), coef(glm2010), coef(glm2012), 
               coef(glm2013), coef(glm2014), coef(glm2016)), 
  SE = c(summary(glm2003)$coefficient[,2], summary(glm2005)$coefficient[,2], 
         summary(glm2007)$coefficient[,2], summary(glm2009)$coefficient[,2], 
         summary(glm2010)$coefficient[,2], summary(glm2012)$coefficient[,2], 
         summary(glm2013)$coefficient[,2], summary(glm2014)$coefficient[,2], 
         summary(glm2016)$coefficient[,2])
)
glmAll$Variable <- factor(glmAll$Variable, 
                          levels = c("Intercept", "Ideology", "Ideological Extremism", 
                                     "Partisanship", "Female", "College Education", "Age"))
glmAll_plot <- ggplot(glmAll[glmAll$Variable %in% c("Ideology", "Ideological Extremism"),], 
                      aes(x = as.factor(Year), y = Estimate, 
                          ymin = Estimate - 1.96*SE, 
                          ymax = Estimate + 1.96*SE)) + 
  geom_hline(yintercept = 0, linetype = "dashed",colour = "gray") + 
  geom_pointrange() + facet_wrap(~Variable, nrow = 6, scales = "free") + 
  labs(x = "Year", y = "Logistic Regression Coefficients", 
       title = "Online Activities (JESIII / IV / V)") + 
  theme(plot.title = element_text(hjust = 0.5))
plot(glmAll_plot)
